//unix时间转换
function formatTime(time) {
    let unixtime = time
    let unixTimestamp = new Date(unixtime * 1000)
    let Y = unixTimestamp.getFullYear()
    let M = ((unixTimestamp.getMonth() + 1) > 10 ? (unixTimestamp.getMonth() + 1) : '0' + (unixTimestamp.getMonth() + 1))
    let D = (unixTimestamp.getDate() > 10 ? unixTimestamp.getDate() : '0' + unixTimestamp.getDate())
    let toDay = Y + '-' + M + '-' + D
    return toDay
}
//页首头部数据
const headerStore = new Vuex.Store({
    actions: {
        async getNotify(context) {
            let notifyData = await new Promise((res, rej) => {
                $.ajax({
                    url: mainhost + '/cms/Column/getList',
                    type: 'post',
                    data: {
                        type: 1
                    },
                    dataType: 'json',
                    success: function (data) {
                        res(data)
                    },
                    error: function (err) {
                        rej(err)
                    }
                })
            }).then(data => {
                return data
            }).catch(err => {
                return err
            })
            context.commit('getNotify', notifyData)
        }
    },
    mutations: {
        getNotify(state, payload) {
            let fixHtml = payload.data
                .map(item=>{
                    return `
                        <div>
                            <h4>${item.title}</h4>
                            <div>${item.ckcontent}</div>
                            <p>${formatTime(item.dateline)}</p>
                        </div>
                    `
                })
                .reduce((prev, curr) => {
                    return prev + curr
                })
            state.notifyData = fixHtml
            state.update = new Date().getTime()
        }
    },
    state: {
        notifyData: null,
        update: null
    }
})



//登录页数据
const loginStore = new Vuex.Store({
    actions: {
        async userLogin(context) {
            let account = $('#account')
            let password = $('#password')
            let memberData = await new Promise((res, rej) => {
                $.ajax({
                    url: mainhost + '/member/login/login',
                    type: 'post',
                    data: {
                        username: account.val(),
                        password: password.val()
                    },
                    dataType: 'json',
                    success: function (data) {
                        res(data)
                    },
                    error: function (err) {
                        rej(err)
                    }
                })
            }).then(data => {
                return data
            }).catch(err => {
                return err
            })
            context.commit('userLogin', memberData)
        },
        async getLevel(context) {
            let levelData = await new Promise((res, rej) => {
                $.ajax({
                    url: mainhost + '/member/user/getMemberLevel',
                    type: 'post',
                    dataType:'json',
                    success: function (data) {
                        res(data)
                    },
                    error: function (err) {
                        rej(err)
                    }
                })
            }).then(data => {
                return data
            }).catch(err => {
                return err
            })
            context.commit('getLevel', levelData)
        },
        async getUA(context){
            let UAData = await new Promise((res, rej) => {
                $.ajax({
                    url: mainhost + '/cms/Page/getDetail?id=2',
                    type: 'post',
                    dataType:'json',
                    success: function (data) {
                        res(data)
                    },
                    error: function (err) {
                        rej(err)
                    }
                })
            }).then(data => {
                return data
            }).catch(err => {
                return err
            })
            context.commit('getUA', UAData)
        }
    },
    mutations: {
        userLogin(state, payload) {
            state.loginData = payload
            state.stateCode = payload.code
            state.reload = new Date().getTime()
            if (payload.code == 0) {
                for (key in payload.data) {
                    sessionStorage.setItem(key, payload.data[key])
                }
                state.status = payload.data.status
                state.type = payload.data.type
                state.id = payload.data.memberId
            } else {
                state.type = payload
            }
        },
        getLevel(state, payload) {
            if (payload.code == 0) {
                let levelOpt =
                    payload.data
                    .filter(_data => _data.pid == 0)
                    .map(_data => {
                        _data.label = _data.title
                        _data.value = _data.id
                        _data.children =
                            payload.data
                            .filter(__data => __data.pid == _data.id)
                            .map(__data => {
                                __data.label = __data.title
                                __data.value = __data.id
                                return __data
                            })
                        return _data
                    })
                state.levelOpt = levelOpt
            } else {
                state.levelOpt = payload
            }
        },
        getUA(state, payload) {
            state.uaData = payload
        }
    },
    state: {
        loginData: null,
        id: null,
        type: null,
        status: null,
        stateCode: null,
        levelOpt: [],
        reload: null,
        uaData: {}
    },
    getters: {

    }
})

//首页的数据
const homeStore = new Vuex.Store({
    actions: {
        async getMemberInfo(context) {
            let userid = sessionStorage.getItem('memberId')
            let memberData = await new Promise((res,rej)=>{
                $.ajax({
                    url: mainhost + '/member/user/getMemberInfo',
                    type: 'post',
                    data: {
                        memberId: userid
                    },
                    dataType: 'json',
                    success: function (data) {
                        res(data)
                    },
                    error: function (err) {
                        rej(err)
                    }
                })
            })
            context.commit('getMemberInfo',memberData)
        },
        async getHomeData(context) {
            let userid = sessionStorage.getItem('memberId')
            let homeData = await new Promise((res, rej) => {
                $.ajax({
                    url: mainhost + '/count/index/index',
                    type: 'post',
                    data: {
                        id: userid
                    },
                    dataType: 'json',
                    success: function (data) {
                        res(data)
                    },
                    error: function (err) {
                        rej(err)
                    }
                })
            }).then(data => {
                return data
            }).catch(err => {
                return err
            })
            context.commit('getHomeData', homeData)
        },
        async getIncomeData(context) {
            let userid = sessionStorage.getItem('memberId')
            let incomeData = await new Promise((res, rej) => {
                $.ajax({
                    url: mainhost + '/member/Moneylog/index',
                    type: 'post',
                    data: {
                        memberId: userid,
                        page: 1,
                        pageSize: 20,
                    },
                    dataType: 'json',
                    success: function (data) {
                        res(data)
                    },
                    error: function (err) {
                        rej(err)
                    }
                })
            }).then(data => {
                return data
            }).catch(err => {
                return err
            })
            context.commit('getIncomeData', incomeData)
        },
        async getDetailData(context) {
            let userid = sessionStorage.getItem('memberId')
            let incomeData = await new Promise((res, rej) => {
                $.ajax({
                    url: mainhost + '/count/index/report',
                    type: 'post',
                    data: {
                        memberId: userid
                    },
                    dataType: 'json',
                    success: function (data) {
                        res(data)
                    },
                    error: function (err) {
                        rej(err)
                    }
                })
            }).then(data => {
                return data
            }).catch(err => {
                return err
            })
            context.commit('getDetailData', incomeData)
        }
    },
    mutations: {
        getMemberInfo(state, payload) {
            state.memberData = payload
            return
        },
        getHomeData(state, payload) {
            state.homeData = payload
            return
        },
        getIncomeData(state, payload) {
            state.incomeData = payload
            return
        },
        getDetailData(state, payload) {
            state.detailData = payload
            return
        }
    },
    state: {
        memberData: {

        },
        homeData: {
            data: {
                userId: null,
                balance: null
            }
        },
        incomeData: [],
        detailData: {
            data: []
        },
    },
    getters: {

    }
})


//推广页的数据
const resultStore = new Vuex.Store({
    actions: {
        async getResultData(context) {
            let resultData = await new Promise((res, rej) => {
                let userid = sessionStorage.getItem('memberId')
                $.ajax({
                    url: mainhost + '/count/Spread/index',
                    type: 'post',
                    data: {
                        memberId: userid
                    },
                    dataType: 'json',
                    success: function (data) {
                        res(data)
                    },
                    error: function (err) {
                        rej(err)
                    }
                })
            }).then(data => {
                return data
            }).catch(err => {
                return err
            })
            context.commit('getResultData', resultData)
        },
        async getIncomeData(context) {
            let userid = sessionStorage.getItem('memberId')
            let incomeData = await new Promise((res, rej) => {
                $.ajax({
                    url: mainhost + '/member/Moneylog/index',
                    type: 'post',
                    data: {
                        memberId: userid,
                        page: 1,
                        pageSize: 20,
                    },
                    dataType: 'json',
                    success: function (data) {
                        res(data)
                    },
                    error: function (err) {
                        rej(err)
                    }
                })
            }).then(data => {
                return data
            }).catch(err => {
                return err
            })
            context.commit('getIncomeData', incomeData)
        },
    },
    mutations: {
        getResultData(state, payload) {
            state.resultData = payload.data
            return
        },
        getIncomeData(state, payload) {
            state.incomeData = payload
            return
        },
    },
    state: {
        incomeData: [],
        resultData: {
            spread: {
                allTodaySpread: {
                    installTimeCount: null
                }
            },
            star: {
                allStar: [{
                    username: '',
                    avatar: '',
                    money: ''
                }, {
                    username: '',
                    avatar: '',
                    money: ''
                }, {
                    username: '',
                    avatar: '',
                    money: ''
                }],
                monthStar: [{
                    username: '',
                    avatar: '',
                    money: ''
                }, {
                    username: '',
                    avatar: '',
                    money: ''
                }, {
                    username: '',
                    avatar: '',
                    money: ''
                }],
                weekStar: [{
                    username: '',
                    avatar: '',
                    money: ''
                }, {
                    username: '',
                    avatar: '',
                    money: ''
                }, {
                    username: '',
                    avatar: '',
                    money: ''
                }]
            }
        }
    },
    getters: {

    }
})


//推广素材
const infoStore = new Vuex.Store({
    actions: {
        async getMaterial(context) {
            let materialData = await new Promise((res, rej) => {
                let userid = sessionStorage.getItem('memberId')
                $.ajax({
                    url: mainhost + '/count/spread/material',
                    type: 'post',
                    data: {
                        memberId: userid
                    },
                    dataType: 'json',
                    success: function (data) {
                        res(data)
                    },
                    error: function (err) {
                        rej(err)
                    }
                })
            }).then(data => {
                return data
            }).catch(err => {
                return err
            })
            context.commit('getMaterial', materialData)
        },
        async getTutorial(context) {
            let materialData = await new Promise((res, rej) => {
                let userid = sessionStorage.getItem('memberId')
                $.ajax({
                    url: mainhost + '/cms/Column/getList?type=2',
                    type: 'post',
                    data: {
                        memberId: userid
                    },
                    dataType: 'json',
                    success: function (data) {
                        res(data)
                    },
                    error: function (err) {
                        rej(err)
                    }
                })
            }).then(data => {
                return data
            }).catch(err => {
                return err
            })
            context.commit('getTutorial', materialData)
        }
    },
    mutations: {
        getMaterial(state, payload) {
            state.material = payload.data
        },
        getTutorial(state, payload) {
            state.tutorial = payload.data
        }
    },
    state: {
        material: {},
        tutorial: {

        }
    }
})


//个人中心数据
const selfStore = new Vuex.Store({
    actions: {
        getSelfData(context) {
            context.commit('getSelfData')
        },
        async getIncomeData(context) {
            let userid = sessionStorage.getItem('memberId')
            let incomeData = await new Promise((res, rej) => {
                $.ajax({
                    url: mainhost + '/member/Moneylog/index',
                    type: 'post',
                    data: {
                        memberId: userid,
                        page: 1,
                        pageSize: 20,
                    },
                    dataType: 'json',
                    success: function (data) {
                        res(data)
                    },
                    error: function (err) {
                        rej(err)
                    }
                })
            }).then(data => {
                return data
            }).catch(err => {
                return err
            })
            context.commit('getIncomeData', incomeData)
        },
        async getNotify(context) {
            let notifyData = await new Promise((res, rej) => {
                $.ajax({
                    url: mainhost + '/cms/Column/getList',
                    type: 'post',
                    data: {
                        type: 1
                    },
                    dataType: 'json',
                    success: function (data) {
                        res(data)
                    },
                    error: function (err) {
                        rej(err)
                    }
                })
            }).then(data => {
                return data
            }).catch(err => {
                return err
            })
            context.commit('getNotify', notifyData)
        }
    },
    mutations: {
        getSelfData(state) {
            let userid = sessionStorage.getItem('memberId')
            $.ajax({
                url: mainhost + '/member/user/getMemberInfo',
                type: 'post',
                data: {
                    memberId: userid
                },
                dataType: 'json',
                success: function (data, status, xhr) {
                    console.log(data)
                    state.selfData = data.data
                }
            })
            return
        },
        getIncomeData(state, payload) {
            state.incomeData = payload
            return
        },
        getNotify(state, payload) {
            let fixHtml = payload.data
                .map(item=>{
                    return `
                        <div>
                            <h4>${item.title}</h4>
                            <div>${item.ckcontent}</div>
                            <p>${formatTime(item.dateline)}</p>
                        </div>
                    `
                })
                .reduce((prev, curr) => {
                    return prev + curr
                })
            state.notifyData = fixHtml
            state.update = new Date().getTime()
        },
    },
    state: {
        selfData: {
            member: {
                avatar: '',
                balance: '',
                bank: '',
                bank_no: '',
                id: '',
                phone: '',
                real_name: '',
                report_adddress: '',
                report_secret: '',
                sex: '',
                username: '',
            }
        },
        incomeData: [],
        notifyData: null,
        update: null
    },
    getters: {

    }
})



module.exports = {
    headerStore,
    loginStore,
    homeStore,
    resultStore,
    selfStore,
    infoStore,
}